package com.sigmatrix.h5.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.sigmatrix.h5.config.SpringContextHelper;
import com.sigmatrix.h5.web.Result;

/**
 * 测试接口prod 环境拦截。
 * 
 * @author wei.wang
 *
 */
@Component
public class TestUtilInterceptor implements HandlerInterceptor {
	
	private static Logger logger = LoggerFactory.getLogger(TestUtilInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if(SpringContextHelper.isprod()){
			if(request.getRequestURI().contains("/util/test")){
				response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
				response.getWriter().write(Result.error(HttpStatus.SC_UNAUTHORIZED + "", "不可访问").toString());
				return false;
			}
		}
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	

}
